<script type="text/html" template>
    <link rel="stylesheet" href="{{ layui.setter.base }}style/login.css?v={{ layui.admin.v }}-1" media="all">
</script>


<div class="layadmin-user-login layadmin-user-display-show" id="LAY-user-login" style="display: none;">

    <div class="layadmin-user-login-main">
        <div class="layadmin-user-login-box layadmin-user-login-header">
            <p><img src="/notepress-admin/static/assets/image/notepress-lg.png" style="height: 80px;"></p>
        </div>
        <div class="layadmin-user-login-box layadmin-user-login-body layui-form">
            <div class="layui-form-item">
                <label class="layadmin-user-login-icon layui-icon layui-icon-username" for="LAY-user-login-username"></label>
                <input type="text" name="username" id="LAY-user-login-username" lay-verify="required|username" placeholder="用户名" class="layui-input">
            </div>
            <div class="layui-form-item">
                <label class="layadmin-user-login-icon layui-icon layui-icon-password" for="LAY-user-login-password"></label>
                <input type="password" name="password" id="LAY-user-login-password" lay-verify="required|pass" placeholder="密码" class="layui-input">
            </div>
            <div class="layui-form-item">
                <div class="layui-row">
                    <div class="layui-col-xs7">
                        <label class="layadmin-user-login-icon layui-icon layui-icon-vercode" for="LAY-user-login-vercode"></label>
                        <input type="text" name="code" id="LAY-user-login-vercode" lay-verify="required" placeholder="图形验证码" class="layui-input" maxlength="4" minlength="4">
                    </div>
                    <div class="layui-col-xs5">
                        <div style="margin-left: 10px;">
                            <img class="layadmin-user-login-codeimg" id="LAY-user-get-vercode">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item" style="margin-bottom: 20px;">
            </div>
            <div class="layui-form-item">
                <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="LAY-user-login-submit">登 入</button>
            </div>

        </div>
    </div>

    <div class="layui-trans layadmin-user-login-footer">

        <p>© 2020 <a href="wuwenbin.me/content/u/notepress" target="_blank">部署文档</a></p>
        <p>
            <span><a href="//wuwenbin.me/content/u/updateLog" target="_blank">更新日志</a></span>
        </p>
    </div>


</div>
<script src="/notepress-admin/static/plugins/md5/md5.min.js"></script>
<script>
    layui.use(['form', 'admin'], function () {
        var $ = layui.$
            , layer = layui.layer
            , laytpl = layui.laytpl
            , setter = layui.setter
            , view = layui.view
            , admin = layui.admin
            , form = layui.form
            , router = layui.router()
            , search = router.search;

        var $body = $('body');

        form.verify({
            username: function (value) {
                if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                    return '用户名不能有特殊字符';
                }
                if (/(^\_)|(\__)|(\_+$)/.test(value)) {
                    return '用户名首尾不能出现下划线\'_\'';
                }
                if (/^\d+\d+\d$/.test(value)) {
                    return '用户名不能全为数字';
                }
                if (value.length < 4 || value.length > 12) {
                    return "用户名必须4~12位，且不能包含空格";
                }
            }
            , pass: function (value) {
                if (!/^[\S]{6,12}$/.test(value)) {
                    return '密码必须6到12位，且不能出现空格';
                }
            }
        });

        var $vcode = $("#LAY-user-get-vercode");

        //提交
        form.on('submit(LAY-user-login-submit)', function (obj) {
            if (obj && obj.field && obj.field.password) {
                obj.field.password = md5(obj.field.password);
            }
            //请求登入接口
            admin.req({
                url: NP_Config.domain + '/admin/login'
                , type: 'post'
                , data: obj.field
                , success: function (res) {
                    if (res.code === NotePress.Constant.CODE_SUCCESS) {
                        //请求成功后，写入 access_token
                        layui.data(setter.tableName, {
                            key: setter.request.tokenName
                            , value: res.access_token
                        });

                        //登入成功的提示与跳转
                        layer.msg('登入成功', {
                            offset: '15px'
                            , icon: 1
                            , time: 1000
                        }, function () {
                            location.hash = (search.redirect ? decodeURIComponent(search.redirect) : '/');
                        });
                    } else {
                        // view.error(res.message);
                        $vcode.trigger("click");
                    }

                }
            });

        });


        var src = $vcode.attr("src");
        if (src === '' || src === null || src === undefined) {
            $vcode.attr("src", NP_Config.domain + '/image/code?t=' + new Date().getTime());
        }


        //更换图形验证码
        $body.on('click', '#LAY-user-get-vercode', function () {
            var othis = $(this);
            othis.attr("src", NP_Config.domain + '/image/code?t=' + new Date().getTime());
        });

    });
</script>
